package com.voucher.qiege.one;

import org.apache.http.impl.client.*;
import org.apache.http.client.CookieStore;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.methods.*;

public class SessionManager {
    public static CookieStore cookieStore = new BasicCookieStore();
    public static CloseableHttpClient httpClient = HttpClients.custom()
            .setDefaultCookieStore(cookieStore)
            .setRedirectStrategy(new LaxRedirectStrategy())
            .build();
    public static final HttpClientContext context = HttpClientContext.create();

    // 初始化会话（触发服务器生成JSESSIONID）
    public static void initSession() throws Exception {
        HttpGet getLoginPage = new HttpGet("http://oa.mexingroup.com:9999/login.jsp");
        try (CloseableHttpResponse response = httpClient.execute(getLoginPage, context)) {
            // JSESSIONID已自动存入cookieStore
//            System.out.println("获取的cookie: \n" + cookieStore.getCookies().get(0).toString());
        }
    }

    public static CloseableHttpClient getClient() {
        return httpClient;
    }

    public static void main(String[] args) {
        SessionManager sessionManager = new SessionManager();
        try {
            sessionManager.initSession();        // 初始化会话
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
